//
//  NewPageCtl.m
//  Griezmann
//
//  Created by  Mac on 2017/6/19.
//  Copyright © 2017年  Mac. All rights reserved.
//

#import "NewPageCtl.h"

#import <MAMapKit/MAMapKit.h>
#import "CustomMovingAnnotation.h"

#import <AMapFoundationKit/AMapFoundationKit.h>

#import <AMapSearchKit/AMapSearchKit.h>

#import "MATraceManager.h"

@interface NewPageCtl ()<MAMapViewDelegate,AMapSearchDelegate>
@property (nonatomic, strong) MAMapView *mapView;
@property(nonatomic,strong)AMapSearchAPI * search;
@property(nonatomic,assign)CLLocationCoordinate2D startLocation;
@property(nonatomic,assign)CLLocationCoordinate2D endLocation;
@property (strong, nonatomic) CLLocationManager *locationManager;


@property(nonatomic,strong)NSString * lat;
@property(nonatomic,strong)NSString * lon;
@end

@implementation NewPageCtl

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) -64)];
    self.mapView.delegate = self;
    
    [self.view addSubview:self.mapView];
    
    _mapView.showsUserLocation = YES;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    
    MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init];
    
    r.showsAccuracyRing = NO;
    
    r.showsHeadingIndicator = NO;
    
    [self.mapView updateUserLocationRepresentation:r];
    
    
    self.search = [[AMapSearchAPI alloc] init];
    self.search.delegate = self;


        [self getCurrentLon_lat];
    
    [self initRightBtn];
    
    
    
}


-(void)initRightBtn
{
    UIButton * button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(0, 0, 120, 30);
//    button.backgroundColor = [UIColor colorWithRed:255/255.0 green:211/255.0 blue:155/255.0 alpha:1];
    button.layer.cornerRadius = 5;
    [button setTitle:@"开始" forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(getCurrentLon_lat) forControlEvents:(UIControlEventTouchUpInside)];
    [button setTitleColor:[UIColor colorWithRed:153/255.0 green:50/255.0 blue:204/255.0 alpha:1] forState:(UIControlStateHighlighted)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
}

-(void)start
{

    AMapDrivingRouteSearchRequest *navi = [[AMapDrivingRouteSearchRequest alloc] init];
    
    navi.requireExtension = YES;
    navi.strategy = 5;
    /* 出发点. */
    
    
    
    if (!self.lat) {
        return;
    }
    if (!self.lon) {
        return;
    }
    
    CGFloat lat = [self.lat floatValue];
    CGFloat lon = [self.lon floatValue];
    
    
    navi.origin = [AMapGeoPoint locationWithLatitude:lat
                                           longitude:lon];
    /* 目的地. */
    
//    22.5669200000,113.9532500000
    
    self.endLocation = CLLocationCoordinate2DMake(22.566920, 113.953250);
     navi.destination = [AMapGeoPoint locationWithLatitude:self.endLocation.latitude
                                                longitude:self.endLocation.latitude];
    
    [self.search AMapDrivingRouteSearch:navi];
    
}


/* 路径规划搜索回调. */
- (void)onRouteSearchDone:(AMapRouteSearchBaseRequest *)request response:(AMapRouteSearchResponse *)response
{
    NSLog(@"路径规划 ...............");
    if (response.route == nil)
    {
        return;
    }
    
    //解析response获取路径信息，具体解析见 Demo
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}





-(void)getCurrentLon_lat{
    
    //判定定位是否打开
    if ([CLLocationManager locationServicesEnabled]) {
        _locationManager = [[CLLocationManager alloc]init];
        _locationManager.delegate = self;
        [_locationManager requestAlwaysAuthorization];
        //   currentCity = [NSString string];
        [_locationManager requestWhenInUseAuthorization];
        //设置寻址经度
        _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        _locationManager.distanceFilter = 15.0; // 最小距离
        [_locationManager startUpdatingLocation];
    }
    
}

//在CLLocationManagerDelegate的更新方法中实时获得最新位置信息
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    NSString * lat = [NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];
    NSString * lon = [NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
    
    self.lat = lat;
    self.lon = lon;
    [self start];


}


@end
